import {
  getDistributionData,
  getTeamStats,
  getTeamMembers,
  getCommissionStats,
  getCommissionList,
  applyWithdraw,
  getWithdrawList,
  getMaterialCategories,
  getMaterialList,
  downloadMaterial,
  getDistributionRules,
  applyDistributor,
  getDistributorLevel,
  getUpgradeConditions,
  getDistributorStats
} from '@/api/distribution'

const state = {
  distributionData: null,
  teamStats: null,
  teamMembers: [],
  commissionStats: null,
  commissionList: [],
  withdrawList: [],
  materialCategories: [],
  materialList: [],
  distributionRules: null,
  distributorLevel: null,
  upgradeConditions: [],
  distributorStats: null
}

const mutations = {
  SET_DISTRIBUTION_DATA: (state, data) => {
    state.distributionData = data
  },
  SET_TEAM_STATS: (state, data) => {
    state.teamStats = data
  },
  SET_TEAM_MEMBERS: (state, data) => {
    state.teamMembers = data
  },
  SET_COMMISSION_STATS: (state, data) => {
    state.commissionStats = data
  },
  SET_COMMISSION_LIST: (state, data) => {
    state.commissionList = data
  },
  SET_WITHDRAW_LIST: (state, data) => {
    state.withdrawList = data
  },
  SET_MATERIAL_CATEGORIES: (state, data) => {
    state.materialCategories = data
  },
  SET_MATERIAL_LIST: (state, data) => {
    state.materialList = data
  },
  SET_DISTRIBUTION_RULES: (state, data) => {
    state.distributionRules = data
  },
  SET_DISTRIBUTOR_LEVEL: (state, data) => {
    state.distributorLevel = data
  },
  SET_UPGRADE_CONDITIONS: (state, data) => {
    state.upgradeConditions = data
  },
  SET_DISTRIBUTOR_STATS: (state, data) => {
    state.distributorStats = data
  }
}

const actions = {
  // 获取分销中心数据
  async getDistributionData({ commit }) {
    try {
      const res = await getDistributionData()
      commit('SET_DISTRIBUTION_DATA', res.data)
      return res.data
    } catch (error) {
      console.error('获取分销中心数据失败:', error)
      throw error
    }
  },

  // 获取团队统计
  async getTeamStats({ commit }) {
    try {
      const res = await getTeamStats()
      commit('SET_TEAM_STATS', res.data)
      return res.data
    } catch (error) {
      console.error('获取团队统计失败:', error)
      throw error
    }
  },

  // 获取团队成员列表
  async getTeamMembers({ commit }, params) {
    try {
      const res = await getTeamMembers(params)
      commit('SET_TEAM_MEMBERS', res.data)
      return res.data
    } catch (error) {
      console.error('获取团队成员列表失败:', error)
      throw error
    }
  },

  // 获取佣金统计
  async getCommissionStats({ commit }) {
    try {
      const res = await getCommissionStats()
      commit('SET_COMMISSION_STATS', res.data)
      return res.data
    } catch (error) {
      console.error('获取佣金统计失败:', error)
      throw error
    }
  },

  // 获取佣金明细
  async getCommissionList({ commit }, params) {
    try {
      const res = await getCommissionList(params)
      commit('SET_COMMISSION_LIST', res.data)
      return res.data
    } catch (error) {
      console.error('获取佣金明细失败:', error)
      throw error
    }
  },

  // 申请佣金提现
  async applyWithdraw({ commit }, data) {
    try {
      const res = await applyWithdraw(data)
      return res.data
    } catch (error) {
      console.error('申请佣金提现失败:', error)
      throw error
    }
  },

  // 获取提现记录
  async getWithdrawList({ commit }, params) {
    try {
      const res = await getWithdrawList(params)
      commit('SET_WITHDRAW_LIST', res.data)
      return res.data
    } catch (error) {
      console.error('获取提现记录失败:', error)
      throw error
    }
  },

  // 获取推广素材分类
  async getMaterialCategories({ commit }) {
    try {
      const res = await getMaterialCategories()
      commit('SET_MATERIAL_CATEGORIES', res.data)
      return res.data
    } catch (error) {
      console.error('获取推广素材分类失败:', error)
      throw error
    }
  },

  // 获取推广素材列表
  async getMaterialList({ commit }, params) {
    try {
      const res = await getMaterialList(params)
      commit('SET_MATERIAL_LIST', res.data)
      return res.data
    } catch (error) {
      console.error('获取推广素材列表失败:', error)
      throw error
    }
  },

  // 下载推广素材
  async downloadMaterial({ commit }, id) {
    try {
      const res = await downloadMaterial(id)
      return res.data
    } catch (error) {
      console.error('下载推广素材失败:', error)
      throw error
    }
  },

  // 获取分销规则
  async getDistributionRules({ commit }) {
    try {
      const res = await getDistributionRules()
      commit('SET_DISTRIBUTION_RULES', res.data)
      return res.data
    } catch (error) {
      console.error('获取分销规则失败:', error)
      throw error
    }
  },

  // 申请成为分销商
  async applyDistributor({ commit }, data) {
    try {
      const res = await applyDistributor(data)
      return res.data
    } catch (error) {
      console.error('申请成为分销商失败:', error)
      throw error
    }
  },

  // 获取分销商等级
  async getDistributorLevel({ commit }) {
    try {
      const res = await getDistributorLevel()
      commit('SET_DISTRIBUTOR_LEVEL', res.data)
      return res.data
    } catch (error) {
      console.error('获取分销商等级失败:', error)
      throw error
    }
  },

  // 获取分销商升级条件
  async getUpgradeConditions({ commit }) {
    try {
      const res = await getUpgradeConditions()
      commit('SET_UPGRADE_CONDITIONS', res.data)
      return res.data
    } catch (error) {
      console.error('获取分销商升级条件失败:', error)
      throw error
    }
  },

  // 获取分销商数据统计
  async getDistributorStats({ commit }, params) {
    try {
      const res = await getDistributorStats(params)
      commit('SET_DISTRIBUTOR_STATS', res.data)
      return res.data
    } catch (error) {
      console.error('获取分销商数据统计失败:', error)
      throw error
    }
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
} 